Passed
Push — master ( c3d3e1...e45641 )
by Rafael S.
01:32
created

BitWriter.write24Bit   A

Complexity

Conditions 1
Paths 8

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 6
rs 9.4285
1
/*
2
 * write-bytes: Functions to turn data into bytes.
3
 * Copyright (c) 2017 Rafael da Silva Rocha.
4
 * https://github.com/rochars/byte-data
5
 */
6
7
const floats = require("../src/floats.js");
8
const intBits = require("int-bits");
9
10
const BitWriter = {
11
12
    // https://github.com/majimboo/c-struct
13
    "write48Bit": function (bytes, number, j) {
14
        bytes[j++] = number & 0xFF;
15
        bytes[j++] = number >> 8 & 0xFF;
16
        bytes[j++] = number >> 16 & 0xFF;
17
        bytes[j++] = number >> 24 & 0xFF;
18
        bytes[j++] = number / 0x100000000 & 0xFF;
19
        bytes[j++] = number / 0x10000000000 & 0xFF;
20
        return j;
21
    },
22
23
    // https://github.com/majimboo/c-struct
24
    "write40Bit": function (bytes, number, j) {
25
        bytes[j++] = number & 0xFF;
26
        bytes[j++] = number >> 8 & 0xFF;
27
        bytes[j++] = number >> 16 & 0xFF;
28
        bytes[j++] = number >> 24 & 0xFF;
29
        bytes[j++] = number / 0x100000000 & 0xFF;
30
        return j;
31
    },
32
33
    "write32BitFloat": function (bytes, number, j) {
34
        let bits = intBits.unpack(number);
35
        bytes[j++] = bits & 0xFF;
36
        bytes[j++] = bits >>> 8 & 0xFF;
37
        bytes[j++] = bits >>> 16 & 0xFF;
38
        bytes[j++] = bits >>> 24 & 0xFF;
39
        return j;
40
    },
41
42
    "write32Bit": function (bytes, number, j) {
43
        bytes[j++] = number & 0xFF;
44
        bytes[j++] = number >>> 8 & 0xFF;
45
        bytes[j++] = number >>> 16 & 0xFF;
46
        bytes[j++] = number >>> 24 & 0xFF;
47
        return j;
48
    },
49
50
    "write24Bit": function (bytes, number, j) {
51
        bytes[j++] = number & 0xFF;
52
        bytes[j++] = number >>> 8 & 0xFF;
53
        bytes[j++] = number >>> 16 & 0xFF;
54
        return j;
55
    },
56
57
    "write16Bit": function (bytes, number, j) {
58
        bytes[j++] = number & 0xFF;
59
        bytes[j++] = number >>> 8 & 0xFF;
60
        return j;
61
    },
62
63
    "write16BitFloat": function (bytes, number, j) {
64
        let bits = floats.toHalf(number);
65
        bytes[j++] = bits >>> 8 & 0xFF;
66
        bytes[j++] = bits & 0xFF;
67
        return j;
68
    },
69
70
    "write8Bit": function (bytes, number, j) {
71
        bytes[j++] = number & 0xFF;
72
        return j;
73
    },
74
75
    "write4Bit": function (bytes, number, j) {
76
        bytes[j++] = number & 0xF;
77
        return j;
78
    },
79
80
    "write2Bit": function (bytes, number, j) {
81
        bytes[j++] = number < 0 ? number + 4 : number;
82
        return j;
83
    },
84
85
    "write1Bit": function (bytes, number, j) {
86
        bytes[j++] = number ? 1 : 0;
87
        return j;
88
    },
89
90
    "writeString": function (bytes, string, j) {
91
        bytes[j++] = string.charCodeAt(0);
92
        return j;
93
    },
94
95
    "write64BitFloat": function(bytes, number, j) {
96
        let bits = floats.toFloat64(number);
97
        j = BitWriter["write32Bit"](bytes, bits[1], j);
98
        return BitWriter["write32Bit"](bytes, bits[0], j);
99
    },
100
101
};
102
103
module.exports = BitWriter;
104